package t327.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import t327.mapper.OrderFormMapper;
import t327.mapper.StyleNumberMapper;
import t327.pojo.Client;

import t327.pojo.OrderForm;
import t327.pojo.QuotationList;
import t327.pojo.views.OrderColorSizeClientView;
import t327.pojo.views.OrderListView;
import t327.service.iter.IOrderFromService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class OrderFromServiceImpl implements IOrderFromService {

    @Autowired
    private OrderFormMapper orderFormMapper;
    @Autowired
    private StyleNumberMapper styleNumberMapper;
    @Override
    public HashMap<Object, Object> addOrder(OrderForm orderForm, List<Long> styleNumber) {
        //  StyleNumber styleNumber=new StyleNumber();
        int ofAdd= orderFormMapper.addOrder(orderForm);

        HashMap<Object,Object> map = new HashMap<>();
        Long orderId = orderForm.getOrderId();//订单id
        int osAdd=0;
        int styleNumberId=0;
        for (int i=0;i<styleNumber.size();i++){
            osAdd = styleNumberMapper.OrderOrderStyleRel(orderId,styleNumber.get(i));//添加款号和订单关系表
            styleNumberId = styleNumberMapper.UpdateStyleNumber(styleNumber.get(i));//更新款号id
        }

        if (osAdd>0){
            if (ofAdd>0){
                if(styleNumberId>0){
                    map.put("success","200");
                }else {
                    map.put("false","2005");
                    return map;
                }
            }else {
                map.put("false","2005");
                return map;
            }
        }else {
            map.put("false","205");
            return map;
        }
//        orderFormMapper.OrderStyleNumber();
        return map;
    }



    @Override
    public List<OrderListView> orderList() {
        return orderFormMapper.orderList();
    }

    @Override
    public List<Client> getClientId() {
        return orderFormMapper.getClientId();
    }



    @Override
    public List<Client> getClientIdList(Long clientId) {
        return orderFormMapper.getClientIdList(clientId);
    }




    @Override
    public List<Client> getClientName(String clientName) {
        return orderFormMapper.getClientName(clientName);
    }


    @Override
    public List<OrderColorSizeClientView> renderHomeOrder() {
        return orderFormMapper.renderHomeOrder();
    }

    @Override
    public int UodateOrder(Long orderId) {
        return orderFormMapper.UodateOrder(orderId);
    }


    @Override
    public List<OrderColorSizeClientView> renderHomeOrderNumberQuery(String quotation) {
        return orderFormMapper.renderHomeOrderNumberQuery(quotation);
    }

    @Override
    public List<OrderListView> ConditionQuery(OrderListView orderListView) {
        return orderFormMapper.ConditionQuery(orderListView);
    }

    @Override
    public List<OrderForm> ListButton(Long orderId) {
        return orderFormMapper.ListButton(orderId);
    }

    @Override
    public List<QuotationList> getQuotationList(List<String> styleNumberId) {
        List<QuotationList> quotationList = new ArrayList<>();

        for (int i = 0; i < styleNumberId.size(); i++) {
            boolean b = quotationList.addAll(orderFormMapper.getQuotationList(styleNumberId.get(i)));
        }
        HashMap<Object,Object> map = new HashMap<>();
        if (!quotationList.isEmpty() && quotationList.size() != 0) {
            map.put("out", "成功");
            System.out.println("有数据");
        } else {
            map.put("aaa", "当前款号没有核价单价格");
            System.out.println("没有数据");
        }
      return quotationList;
    }


}
